package com.luoqiz.project.zipFile;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

import org.junit.Test;

//压缩文件、解压文件
public class ZipDome {

	@Test
	public void zipFile() {
		String srcFilePath="D:\\dome\\dest/1.exe";
		String destFilePath="D:\\dome\\dest/11.zip";
		File file=new File(srcFilePath);
		try {
			FileInputStream fin = new FileInputStream(file);
			ZipOutputStream zipout=new ZipOutputStream(new FileOutputStream(destFilePath) );
			System.out.println(file.getName());
			ZipEntry e = new ZipEntry(file.getName());
			zipout.putNextEntry(e);
			int len=-1;
			byte[] b = new byte[1024*1024];
			while((len=fin.read(b))!=-1) {
				zipout.write(b,0,len);
			}
			fin.close();
			zipout.flush();
			zipout.closeEntry();
			zipout.close();
			System.out.println("压缩完成");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
	}
	
	@Test
	public void unzipFile() {
		String srcFile="D:\\dome\\dest/12.zip";
		String destFileDir="D:\\dome\\dest/unzip";
		File destFileDirPath=new File(destFileDir);
		if(!destFileDirPath.exists()) {
			destFileDirPath.mkdirs();
		}
		try {
			ZipInputStream zipin=new ZipInputStream(new FileInputStream(srcFile));
			ZipEntry zipEntry;
			byte[] b=new byte[1024];
			int len=-1;
			int leneg=0;
			while((zipEntry=zipin.getNextEntry())!=null) {
				File file=new File(destFileDir+"/"+zipEntry.getName());
				BufferedOutputStream bout=new BufferedOutputStream(new FileOutputStream(file));
				while((len=zipin.read(b))!=-1) {
					bout.write(b,0,len);
					if((leneg++)==87862) {
						System.out.println(len);
					}
				}
				zipEntry.clone();
				bout.flush();
				bout.close();
				zipEntry.clone();
			}
			zipin.closeEntry();
			zipin.close();
			System.out.println("解压完成");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@Test
	public void zipFileList() {
		String srcFilePath="D:\\dome\\dest/1.exe,D:\\dome\\dest/2.exe";
		String destFilePath="D:\\dome\\dest/12.zip";
		String[] srcFileList=srcFilePath.split(",");
		try {
			ZipOutputStream zipout=new ZipOutputStream(new FileOutputStream(destFilePath) );
			for(String srcFile:srcFileList) {
				File file=new File(srcFile);
				FileInputStream fin = new FileInputStream(file);
				System.out.println(file.getName());
				ZipEntry e = new ZipEntry(file.getName());
				zipout.putNextEntry(e);
				int len=-1;
				byte[] b = new byte[1024];
				while((len=fin.read(b))!=-1) {
					zipout.write(b,0,len);
				}
				
				fin.close();
				zipout.flush();
				zipout.closeEntry();
			}
			zipout.close();
			System.out.println("压缩完成");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
	}
}
